class Solution {
public:
    int calculate(string s) {
        stack<int> num;
        int ch='+';
        int i=0;
        while(i<s.size())
        {   
            if(s[i] == ' ') i++;
            else if(s[i]>='0'&&s[i]<='9')
            {
                int tmp=0;
                while(s[i]>='0'&&s[i]<='9')
                {
                    tmp=(tmp*10)+(s[i]-'0');
                    i++;
                }
                if(ch=='-')
                num.push(-tmp);
                else if(ch=='+')
                num.push(tmp);
                else if(ch=='*')
                num.top()*=tmp;
                else
                num.top()/=tmp;
            }
            else
            {
                ch=s[i++];
            }
        }
        int ret=0;
        while(!num.empty())
        {
            ret+=num.top();
            num.pop();
        }
        return ret;
    }
};